// auth: macro gu
// email&skype: macrogu@hotmail.com

#pragma once

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
#include <thread>
#include <queue>
#include <mutex>

#include "read_conf.h"
#include "log.h"
#include "singleton.h"


#define SOCKET_ERROR -1


// use single-mode  

struct stSendEmailData
{
	std::string cRecvEmail;
	std::string cRecvData;
};

class CEmailSender : public ISingleton<CEmailSender>
{
	public:
		CEmailSender();
		~CEmailSender();

		// push need to be sended data to sending email queue
		void PushBackToEmailSendingQueue(const stSendEmailData& emailData);

	protected:
		// sending email
		bool SendEmail(const std::string& EmailReciver, const std::string& SendData);

	private:
		bool initEmailSender();
		void threadSendEmail();

		bool Send(int& s, std::string data);
		bool Recv(int& s, char* buf, int len);
		std::string Base64Encode(const std::string& src);

		stSmtpInfo m_SMTPInfo;

		int sendEmailLocalSocket;		// the socket used to send email
		char recvBuffer[1024];

		std::thread* SendEmailThread;

		std::queue<stSendEmailData> q_AllNeedSendEmail;
		std::mutex mailMutex;
};
